# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=openshadinglanguage
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=1.8.14
pkgrel=2
pkgdesc="Advanced shading language for production GI renderers (mingw-w64)"
arch=('any')
url="https://github.com/imageworks/OpenShadingLanguage/"
license=("custom")
depends=("${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-clang"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-glew"
         "${MINGW_PACKAGE_PREFIX}-ilmbase"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-openexr"
         "${MINGW_PACKAGE_PREFIX}-openimageio"
         "${MINGW_PACKAGE_PREFIX}-pugixml")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
source=(${_realname}-${pkgver}.tar.gz::https://github.com/imageworks/OpenShadingLanguage/archive/Release-${pkgver}.tar.gz
        0001-install-dirs.patch
        0002-dont-add-flex-include-dir.patch
        0003-fix-OIIO-windows.patch
        0004-fix-installing-docs.patch
        0005-Fix-cast-from-void-ptr.patch
        0007-Fix-bad-casts-with-intptr_t.patch
        0008-openimageio-api-changes.patch
        0009-find-pugixml.patch)
sha256sums=('d77d587fedf8a8abcf60f076beddca3ae25bad1d52a97d7087447452395e1cb4'
            'e3f6ad09619d122ada129cbc77d7778751abfecef9ac7e0a60d9550c811df643'
            'c9e02624cf6e607118f1f7fd8bf8bda9af5103afde5770a11fa5ccf49916fc5b'
            '355120807bab0e7c0ba08d9831ee8e64f4faaedc07a769bad28aca8732880d18'
            'bafdde92611252b061906453a58672245363c9ce2960b175ee27146347086f15'
            '541cc1eb0c506e253de2f26af37dc6e35de891a940906c6e33a2f4774a4a844e'
            'd3e6d2bdd059ec9b1581b02c9f736f26a06e1189929c6276d1d7d81204bb3535'
            'ccf77deca5282a878d05508a56c658cc29efea4e53845fbd508855ecf66a1792'
            'b65fecac8d338f506abf168154ec97baa7523497419f7fdb0a53442ae652adb3')

prepare() {
  cd ${srcdir}/OpenShadingLanguage-Release-${pkgver}
  patch -p1 -i ${srcdir}/0001-install-dirs.patch
  patch -p1 -i ${srcdir}/0002-dont-add-flex-include-dir.patch
  patch -p1 -i ${srcdir}/0003-fix-OIIO-windows.patch
  patch -p1 -i ${srcdir}/0004-fix-installing-docs.patch
  patch -p1 -i ${srcdir}/0005-Fix-cast-from-void-ptr.patch
  patch -p1 -i ${srcdir}/0007-Fix-bad-casts-with-intptr_t.patch
  patch -p1 -i ${srcdir}/0008-openimageio-api-changes.patch
  patch -p1 -i ${srcdir}/0009-find-pugixml.patch
}

build() {
  export OPENIMAGEIOHOME=${MINGW_PREFIX}
  export LLVM_DIRECTORY=${MINGW_PREFIX}
  CFLAGS+=" -std=c++11 -D_USE_MATH_DEFINES"
  CXXFLAGS+=" -std=c++11 -DTINYFORMAT_ALLOW_WCHAR_STRINGS -D_USE_MATH_DEFINES"

  [[ -d build-${MINGW_CHOST} ]] && rm -r build-${MINGW_CHOST}
  mkdir build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DSTOP_ON_WARNING=OFF \
    -DILMBASE_CUSTOM=True \
    -DUSE_EXTERNAL_PUGIXML=ON \
    -DLLVM_STATIC=1 \
    -DOSL_BUILD_CPP11=1 \
    ../OpenShadingLanguage-Release-${pkgver}

    #-DCMAKE_CXX_FLAGS="-DTINYFORMAT_ALLOW_WCHAR_STRINGS"
    #-DVERBOSE=1

  # $PWD/src/liboslcomp PATH addition needed for running oslc.exe during the build (for liboslcomp.dll)
  # ${MINGW_PREFIX}/bin PATH addition needed for finding libboost_system-mt.dll when running oslc.exe.
  PATH=${PWD}/src/liboslcomp:${MINGW_PREFIX}/bin:"${PATH}" make #VERBOSE=1
}

package() {
  cd ${srcdir}//build-${MINGW_CHOST}

  make DESTDIR=${pkgdir} install

  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/OSL/
  mkdir -p "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}
  
  install -Dm644 "${srcdir}/OpenShadingLanguage-Release-${pkgver}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
